

#ifndef _SOFT_IIC_H_
#define _SOFT_IIC_H_



//////////////////

#include"mcu_.h"

typedef enum {
	NACK = 0,
	ACK = 1
}ACK_STATUS;

/// <summary>
/// io口的结构体
/// </summary>
typedef struct _iic_device__
{
	//int io;  // 引脚号
	gpio* scl;
	gpio* sda;
}IICdevice;


// 

////////////////// 下面的引脚操作接口用户定义。

/// <summary>
/// iic scl 引脚设置高电平
/// </summary>
/// <param name="device">IIC 设备</param>
void IIC_SCL_H(IICdevice* device);
/// <summary>
/// iic scl 引脚设置低电平
/// </summary>
/// <param name="device">IIC 设备</param>
void IIC_SCL_L(IICdevice* device);
/// <summary>
/// iic sda 引脚设置高电平
/// </summary>
/// <param name="device">IIC 设备</param>
void IIC_SDA_H(IICdevice* device);
/// <summary>
/// iic sda 引脚设置低电平
/// </summary>
/// <param name="device">IIC 设备</param>
void IIC_SDA_L(IICdevice* device);
/// <summary>
/// 读取iic sda 引脚电平
/// </summary>
/// <param name="device">IIC 设备</param>
/// <returns>0低电平，1高电平</returns>
int IIC_SDA_READ(IICdevice* device);

/// <summary>
/// SDA引脚设置输出模式
/// </summary>
/// <param name="device">IIC 设备</param>
void Soft_IIC_Output(IICdevice* device);

/// <summary>
/// SDA引脚设置输入模式
/// </summary>
/// <param name="device">IIC 设备</param>
void Soft_IIC_Input(IICdevice* device);
//

////////////////////////////////////////////


/// <summary>
/// 初始化IIC
/// </summary>
/// <param name="device"></param>
void Soft_IIC_Init(IICdevice* device);
/// <summary>
/// 启动IIC
/// </summary>
/// <param name="device"></param>
void Soft_IIC_Start(IICdevice* device);
/// <summary>
/// 停止IIC
/// </summary>
/// <param name="device"></param>
void Soft_IIC_Stop(IICdevice* device);
/// <summary>
/// IIC写入1字节数据
/// </summary>
/// <param name="device"></param>
/// <param name="Byte">数据</param>
/// <returns>是否有应答</returns>
ACK_STATUS Soft_IIC_Send_Byte(IICdevice* device, unt8 Byte);
/// <summary>
/// 读取1字节IIC串口数据。
/// </summary>
/// <param name="device"></param>
/// <param name="ack_sta">读取数据后是否应答</param>
/// <returns>读取的数据</returns>
unt8 Soft_IIC_Recv_Byte(IICdevice* device, ACK_STATUS ack_sta);
/// <summary>
/// 向地址是IC_Address的I2C设备写入dat_Length个数据dat。如果发送成功，返回true，否则返回false。
/// </summary>
/// <param name="device"></param>
/// <param name="IC_Address">IIC设备地址</param>
/// <param name="dat">数据数组</param>
/// <param name="dat_Length">数据长度</param>
/// <returns>是否写入成功</returns>
bool Soft_IIC_Send_Bytes(IICdevice* device, unt8 IC_Address, unt8* dat, int dat_Length);

/// <summary>
/// 从地址是IC_Address的I2C设备读取dat_Length个数据dat。如果读取成功，返回true，否则返回false。
/// </summary>
/// <param name="device"></param>
/// <param name="IC_Address">IIC设备地址</param>
/// <param name="dat">数据数组</param>
/// <param name="dat_Length">数据长度</param>
/// <returns>是否读取成功</returns>
bool Soft_IIC_Recv_Bytes(IICdevice* device, unt8 IC_Address, unt8* dat, int dat_Length);



/// <summary>
/// 往IIC设备地址是IC_Address的器件写入数据，设备寄存器地址是addr，地址长度是1或2，（8位地址或16位地址）
/// 写入数据是buf[],数组长度是buf_len
/// </summary>
/// <param name="IC_Address">IIC设备地址</param>
/// <param name="addr">寄存器地址</param>
/// <param name="addr_len">地址长度,=1或=2</param>
/// <param name="buf">数据缓存</param>
/// <param name="buf_len">缓存长度</param>
void Soft_IIC_write(IICdevice* device, unt8 IC_Address, unt16 addr, unt8 addr_len, unt8* buf, unt16 buf_len);

/// <summary>
/// 从IIC设备地址是IC_Address的器件读出数据，设备寄存器地址是addr，地址长度是1或2，（8位地址或16位地址）
/// 读出数据是buf[],长度是buf_len
/// </summary>
/// <param name="IC_Address">IIC设备地址</param>
/// <param name="IC_Address">IIC设备地址</param>
/// <param name="addr">寄存器地址</param>
/// <param name="addr_len">地址长度,=1或=2</param>
/// <param name="buf">数据缓存</param>
/// <param name="buf_len">缓存长度</param>
void Soft_IIC_read(IICdevice* device, unt8 IC_Address, unt16 addr, unt8 addr_len, unt8* buf, unt16 buf_len);

//





#endif










